package com.moli.iov.third.invocation.op;

import com.alibaba.fastjson.JSONObject;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.third.config.OpProperties;
import com.moli.iov.third.contstants.Constants;
import com.moli.iov.third.invocation.op.base.OpBaseHandler;
import com.moli.iov.third.model.dto.op.ModifyUserDto;
import com.moli.iov.util.StringUtil;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 修改用户
 * </p>
 *
 * @author zql
 * @since 2019/10/10 10:15
 */
@Slf4j
@Component
public class ModifyUserHandler {

  @Autowired
  private OpBaseHandler opBaseHandler;

  @Autowired
  private OpProperties opProperties;

  /**
   * 修改用户通知op
   * @param modifyUserDto
   */
  public void modifyUser(ModifyUserDto modifyUserDto) throws IOException {

    String result =
        opBaseHandler.post(opProperties.getModifyUserUrl(), JSONObject.toJSONString(modifyUserDto));

    if(StringUtil.isBlank(result)){
      log.error("修改用户通知OP， OP返回空");
      throw new BusinessException("修改用户通知OP失败");
    }

    JSONObject jsonObject = JSONObject.parseObject(result);

    if(Constants.OP_SUCCESS != jsonObject.getInteger("code")){
      log.error("修改用户通知OP失败，OP返回状态非{} 出参：{}", Constants.OP_SUCCESS, jsonObject);
      throw new BusinessException(jsonObject.getString("message"));
    }

    log.info("修改用户通知OP 成功 参数：{}", modifyUserDto);
  }
}
